package com.visikard.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.visikard.ui.R;
import com.visikard.ui.fragment.CouponReviewFragment;
import com.visikard.ui.fragment.GiveCouponToAudience;
import com.visikard.ui.fragment.ListAudienceFragment;
import com.visikard.ui.fragment.ListCouponsRTUIFragment;
import com.visikard.ui.fragment.ViewClickable;

public class CouponsRTUIMainActivity extends BaseLoginCompleteActivity implements ViewClickable {

	private FragmentManager fmManager;
	private FragmentTransaction fmTransaction;
	
	private Fragment saveFg;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.coupons_rtui_main_activity);
		fmManager = getSupportFragmentManager();
		
		putFragment(new ListCouponsRTUIFragment(), "ListCouponsRTUIFragment");
	}
	
	/**
	 * put a fragment with UI
	 */
	public void putFragment(Fragment fm, String tag) {
		saveFg = fm;
//		if(fmManager.findFragmentByTag(tag)== null){
			fmTransaction = fmManager.beginTransaction();
			fmTransaction.setCustomAnimations(R.anim.push_left_in, 0, 0, R.anim.push_right_out);
			fmTransaction.add(R.id.coupons_content_cover, fm);
			fmTransaction.addToBackStack(null);
			fmTransaction.commit();
//		}
	}
	
	/**
	 * put a fragment with UI
	 */
	public void putFragmentDetails(Fragment fm) {
		fmTransaction = fmManager.beginTransaction();
		fmTransaction.setCustomAnimations(R.anim.push_left_in, 0, 0, R.anim.push_right_out);
		fmTransaction.add(R.id.coupons_content_cover, fm);
		fmTransaction.addToBackStack(null);
		fmTransaction.commit();
	}
	
	public void removeFragment(){
		if(saveFg != null)
			fmManager.beginTransaction().remove(saveFg).commit();
	}
	
	@Override
	public void onViewClick(View v) {
		if(saveFg != null && saveFg instanceof ViewClickable){
			((ViewClickable)saveFg).onViewClick(v);
		}
	}

	@Override
	public void onBackPressed() {
		Fragment fm = fmManager.findFragmentById(R.id.coupons_content_cover);
		if(fm instanceof ListCouponsRTUIFragment)
			finish();
		else if(fm instanceof GiveCouponToAudience)
			super.onBackPressed();
		else if(fm instanceof ListAudienceFragment)
			super.onBackPressed();
		else if(fm instanceof CouponReviewFragment)
			super.onBackPressed();
	}
}
